<?php
class Template
{
    private $file = null;
    private $vars = array();

    public function __construct($file = null)
    {
        $this->file = $file;
    }

	public function set_tpl($file)
	{
		$this->file = $file;
	}

	public function get_tpl() 
	{
		return $this->file;
	}

    public function set($name, $value = '')
    {
        if (!is_array($name)) {
            $this->vars[$name] = is_object($value) ? $value->fetch() : $value;
        } else {
            foreach ($name as $pls_name => $pls_value) {
                $this->vars[$pls_name] = is_object($pls_value) ? $pls_value->fetch() : $pls_value;
            }
        }
    }

    public function fetch($file = null)
    {
        $file = empty($file) ? $this->file : $file;

        if (is_array($this->vars) && !empty($this->vars)) {
            extract($this->vars);
        }

        ob_start();
        include($file);
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    
    public function __destruct()
    {
    	unset($this->file);
    	unset($this->vars);
    }
}
?>